package com.myorg.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.compress.CodecPool;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.Decompressor;
import org.apache.hadoop.util.ReflectionUtils;

public class PooledStreamDecompressor {
	public static void main(String[] args) throws Exception {
		String codecClassname = "org.apache.hadoop.io.compress.GzipCodec";

		Class<?> codecClass = Class.forName(codecClassname);
		Configuration conf = new Configuration();
		CompressionCodec codec = (CompressionCodec) ReflectionUtils
				.newInstance(codecClass, conf);
		Decompressor decompressor = null;
		try {
			File file = new File("/home/mma/newfile.txt");
			FileInputStream fip = new FileInputStream(file);

			decompressor = CodecPool.getDecompressor(codec);
			CompressionInputStream cis = codec.createInputStream(fip,
					decompressor);
			// BufferedInputStream bis =new BufferedInputStream(cis);

			String line = "";
			BufferedReader rd = new BufferedReader(new InputStreamReader(cis));
			while ((line = rd.readLine()) != null) {
				System.out.println(line);
			}
			rd.close();
			fip.close();
			System.out.println("completed!");
		} finally {
			CodecPool.returnDecompressor(decompressor);
		}
	}
}
